স্প্রিং বুটে WebClient ব্যবহার করে Basic এবং Bearer Token Authentication করার পদ্ধতি নিচে বিস্তারিতভাবে উদাহরণসহ দেখানো হয়েছে।
১. Basic Authentication কনফিগারেশন
Basic Authentication-এ ব্যবহারকারী নাম এবং পাসওয়ার্ড সরাসরি HTTP হেডারে Authorization ফিল্ডে Base64 এনকোডেড ফরম্যাটে পাঠানো হয়।
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class BasicAuthWebClient {
private final WebClient webClient;
public BasicAuthWebClient(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder
.baseUrl("https://api.example.com")
.defaultHeaders(headers -> headers.setBasicAuth("username", "password"))
.build();
}
public String getProtectedResource() {
return webClient.get()
.uri("/protected-resource")
.retrieve()
.bodyToMono(String.class)
.block();
}
}
২. ডিফল্ট WebClient Bean তৈরি
অন্য যেকোনো জায়গায় Basic Authentication যুক্ত করতে চাইলে এটি Bean আকারে ব্যবহার করা যায়।
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfig {
@Bean
public WebClient basicAuthWebClient() {
return WebClient.builder()
.baseUrl("https://api.example.com")
.defaultHeaders(headers -> headers.setBasicAuth("username", "password"))
.build();
}
}
WebClient এর মাধ্যমে Bearer Token Authentication
১. Bearer Token Authentication কনফিগারেশন
Bearer Token ব্যবহার করে সুরক্ষিত API অ্যাক্সেস করার সময় টোকেন Authorization হেডারে Bearer <token> ফরম্যাটে পাঠানো হয়।
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class BearerTokenWebClient {
private final WebClient webClient;
public BearerTokenWebClient(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder
.baseUrl("https://api.example.com")
.defaultHeaders(headers -> headers.setBearerAuth("your-access-token"))
.build();
}
public String getProtectedResource() {
return webClient.get()
.uri("/protected-resource")
.retrieve()
.bodyToMono(String.class)
.block();
}
}
২. ডায়নামিকভাবে টোকেন যোগ করা
যদি টোকেন ডায়নামিকভাবে সেট করতে হয়, তবে রিকোয়েস্টের সময় হেডারে টোকেন যোগ করা যায়।
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class DynamicBearerTokenWebClient {
private final WebClient webClient;
public DynamicBearerTokenWebClient(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder
.baseUrl("https://api.example.com")
.build();
}
public String getProtectedResource(String accessToken) {
return webClient.get()
.uri("/protected-resource")
.headers(headers -> headers.setBearerAuth(accessToken))
.retrieve()
.bodyToMono(String.class)
.block();
}
}
পূর্ণ উদাহরণ: Basic এবং Bearer Token Authentication
কন্ট্রোলার থেকে কল করা
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebClientController {
@Autowired
private BasicAuthWebClient basicAuthWebClient;
@Autowired
private BearerTokenWebClient bearerTokenWebClient;
@Autowired
private DynamicBearerTokenWebClient dynamicBearerTokenWebClient;
@GetMapping("/basic-auth")
public String callWithBasicAuth() {
return basicAuthWebClient.getProtectedResource();
}
@GetMapping("/bearer-token")
public String callWithBearerToken() {
return bearerTokenWebClient.getProtectedResource();
}
@GetMapping("/dynamic-bearer-token")
public String callWithDynamicBearerToken() {
String token = "your-access-token"; // টোকেন সংগ্রহের উপায় অনুযায়ী ডাইনামিক টোকেন ব্যবহার করুন
return dynamicBearerTokenWebClient.getProtectedResource(token);
}
}
গুরুত্বপূর্ণ পয়েন্ট
- Basic Authentication:
- Base64 এনকোডিংয়ে সংবেদনশীল তথ্য (username এবং password) সুরক্ষিত নয়। এটি SSL/TLS-সহ ব্যবহার করুন।
- Bearer Token:
- টোকেনটি ডাইনামিকভাবে সংগ্রহ করুন (যেমন OAuth2 ফ্লো ব্যবহার করে) এবং নিরাপদে সংরক্ষণ করুন।
- Exception Handling:
WebClient-এরonStatusব্যবহার করে HTTP ত্রুটি হ্যান্ডলিং যুক্ত করুন।
Error Handling উদাহরণ
return webClient.get()
.uri("/protected-resource")
.retrieve()
.onStatus(
HttpStatus::isError,
response -> Mono.error(new RuntimeException("API call failed"))
)
.bodyToMono(String.class)
.block();
উপরের উদাহরণগুলো ব্যবহার করে আপনি WebClient দিয়ে Basic এবং Bearer Token Authentication সঠিকভাবে বাস্তবায়ন করতে পারবেন।